

/**
 * 画图
 */
import DrawPoint from "./draw/DrawPoint"
import DrawPolyline from "./draw/DrawPolyline"
import DrawPolygon from "./draw/DrawPolygon"
import DrawCircle from "./draw/DrawCircle"
import DrawRectangle from "./draw/DrawRectangle"
import DrawPolygonRound from "./draw/DrawPolygonRound"
import DrawViewShed from "./draw/DrawViewShed"

/**
 * 编辑
 */
import EditPoint from "./edit/EditPoint"
import EditPolyline from "./edit/EditPolyline"
import EditPolygon from "./edit/EditPolygon"
import EditCircle from "./edit/EditCircle"
import EditRectangle from "./edit/EditRectangle"
import EditPolygonRound from "./edit/EditPolygonRound"
import EditViewShed from "./edit/EditViewShed"


// 图片编辑点
import IMG_CIRCLE_RED from './images/circle_red.png'
import IMG_CIRCLE_BLUE from './images/circle_blue.png'
import IMG_CIRCLE_YELLOW from './images/circle_yellow.png'

// 鼠标提示框
import Tooltips from "../tools/types/Tooltips"

/**
 * 绘制图形
 */
const DEF_OPTS = {
  icon_center: IMG_CIRCLE_YELLOW,
  icon_anchor: IMG_CIRCLE_RED,
  icon_midAnchor: IMG_CIRCLE_BLUE,
  icon_size: [12, 12],
  clampToGround: true
}

export default class Plot {

  constructor(viewer, options = {}) {
    this._viewer = viewer
    this._options = {
      ...DEF_OPTS,
      ...options
    }
    this._plotEvent = new Cesium.Event()
    this._tooltips = new Tooltips(viewer)
    this._callback = undefined
    this._drawWorker = undefined
    this._editWorker = undefined
    this._overlayLayer = new Cesium.CustomDataSource('plot-overlay-layer')
    this._viewer.dataSources.add(this._overlayLayer)
    this._anchorLayer = new Cesium.CustomDataSource('plot-overlay-layer')
    this._viewer.dataSources.add(this._anchorLayer)
    this._state = undefined
    this._viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
    this._viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
  }

  completeCallback(overlay) {
    this._drawWorker = undefined
    this._editWorker = undefined
    
    this._overlayLayer.entities.removeAll()
    this._anchorLayer.entities.removeAll()
    this._callback && this._callback.call(this, overlay)
  }

  bindEvent(callback) {
    this._plotEvent.removeEventListener(this.completeCallback, this)
    this._callback = callback
    this._plotEvent.addEventListener(this.completeCallback, this)
  }

  /**
   * 创建画图工具
   * @param {}} type 
   * @param {*} style 
   */
  createDrawWorker(type, style) {
    switch (type) {
      case 'point':
        this._drawWorker = new DrawPoint(style)
        break
      case 'polyline':
        this._drawWorker = new DrawPolyline(style)
        break
      case 'polygon':
        this._drawWorker = new DrawPolygon(style)
        break
      case 'circle':
        this._drawWorker = new DrawCircle(style)
        break
      case 'rectangle':
        this._drawWorker = new DrawRectangle(style)
        break
      case 'polygonRound':
        this._drawWorker = new DrawPolygonRound(style)        
        break
      case 'viewShed': 
        this._drawWorker = new DrawViewShed(style)        
        break
    }
  }

  /**
   * 创建编辑工具
   * @param {*} overlay 
   */
  createEditWorker(overlay) {

    switch (overlay.type) {
      case 'point':
        this._editWorker = new EditPoint(overlay)
        break
      case 'polyline':
        this._editWorker = new EditPolyline(overlay)
        break
      case 'polygon':
        this._editWorker = new EditPolygon(overlay)
        break
      case 'circle':
        this._editWorker = new EditCircle(overlay)
        break
      case 'rectangle':
        this._editWorker = new EditRectangle(overlay)
        break
      case 'polygonRound':
        this._editWorker = new EditPolygonRound(overlay)
        break
      case 'viewShed':
        this._editWorker = new EditViewShed(overlay)
        break
    }
  }

  /**
   * 
   * @param {*} type 画的类型
   * @param {*} callback 
   * @param {*} style 
   * @param {*} clampToGround 
   * @returns 
   */
  draw(type, callback, style, clampToGround) {
    this._state = 'draw'

    if (this._drawWorker) {
      this._drawWorker.unbindEvent()
      this._drawWorker = undefined
    }
    this.bindEvent(callback)
    this.createDrawWorker(type, style)
    this._drawWorker &&
      this._drawWorker.start(this, clampToGround ?? this._options.clampToGround)

    return this
  }

  edit(overlay, callback, clampToGround) {
    this._state = 'edit'
    if (this._editWorker) {
      this._editWorker.unbindEvent()
      this._editWorker = undefined
    }
    
    this.bindEvent(callback)
    this.createEditWorker(overlay)
    this._editWorker &&
      this._editWorker.start(this, clampToGround ?? this._options.clampToGround)
    return this
  }

  get viewer() {
    return this._viewer
  }

  get options() {
    return this._options
  }

  get plotEvent() {
    return this._plotEvent
  }

  get overlayLayer() {
    return this._overlayLayer.entities
  }

  get anchorLayer() {
    return this._anchorLayer.entities
  }

  get tooltips(){
    return this._tooltips
  }

  get state() {
    return this._state
  }

}